import { getBlobFileByUrl } from '@/apis/common/common';
import { downloadFile, getFileNameAndSuffixByUrl } from '@/utils/file';

export const useDownload = () => {
  async function downloadByUrl(fileUrl: string) {
    try {
      const { data } = await getBlobFileByUrl(fileUrl);
      const { filename, suffix } = getFileNameAndSuffixByUrl(fileUrl);
      downloadFile(data, filename, suffix);
      return Promise.resolve(true);
    } catch (error) {
      return Promise.resolve(false);
    }
  }

  return {
    downloadByUrl
  };
};
